#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

void sleep(int msec)
{
    QTime _Timer = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < _Timer )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    motor = new Moto;
    motor->init(1, 2);

    connect(&sensorCom, SIGNAL(readyRead()), this, SLOT(readSensorCom()));
    connect(&gpsCom,    SIGNAL(readyRead()), this, SLOT(readGpsCom()));
    connect(&ipcCom,    SIGNAL(readyRead()), this, SLOT(readIpcCom()));

    gpsCom.setPortName("ttyUSB0");
    gpsCom.setBaudRate(QSerialPort::Baud4800);     //设置波特率
    gpsCom.setDataBits(QSerialPort::Data8);        //设置数据位
    gpsCom.setParity(QSerialPort::NoParity);       //设置校验
    gpsCom.setStopBits(QSerialPort::OneStop);      //设置停止位
    gpsCom.setFlowControl(QSerialPort::NoFlowControl);

    if(gpsCom.open(QIODevice::ReadOnly))
    {

    }

    sensorCom.setPortName("ttyS0");
    sensorCom.setBaudRate(QSerialPort::Baud38400);     //设置波特率
    sensorCom.setDataBits(QSerialPort::Data8);        //设置数据位
    sensorCom.setParity(QSerialPort::NoParity);       //设置校验
    sensorCom.setStopBits(QSerialPort::OneStop);      //设置停止位
    sensorCom.setFlowControl(QSerialPort::NoFlowControl);

    if(sensorCom.open(QIODevice::ReadWrite))
    {

    }

    ipcCom.setPortName("ttyUSB1");
    ipcCom.setBaudRate(QSerialPort::Baud38400);     //设置波特率
    ipcCom.setDataBits(QSerialPort::Data8);        //设置数据位
    ipcCom.setParity(QSerialPort::NoParity);       //设置校验
    ipcCom.setStopBits(QSerialPort::OneStop);      //设置停止位
    ipcCom.setFlowControl(QSerialPort::NoFlowControl);

    if(ipcCom.open(QIODevice::ReadWrite))
    {

    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_spinBox_valueChanged(int arg1)
{
    motor->setSpeed(arg1);
}

void MainWindow::on_radioButton_clicked()
{

}

void MainWindow::on_radioButton_2_clicked()
{

}

void MainWindow::readGpsCom()
{
    static QByteArray buf;

    buf += gpsCom.readAll();
    if(buf.indexOf("\r\n") != -1)
    {
        qDebug() << buf;
        gps.decodeNMEA(buf);
        ui->label->setText("时间：" + gps.datetime.toString("yyyy-MM-dd hh:mm:ss"));
        ui->label_2->setText("坐标：" + QString::number(gps.latitude, 'f', 4) + " , " + QString::number(gps.longitude, 'f', 4));
        ui->label_3->setText("航向：" + QString::number(gps.course, 'f', 1));

        if(gps.state)
            ui->label_4->setText("定位状态：有效");
        else
            ui->label_4->setText("定位状态：无效");

        buf.clear();
    }
}

void MainWindow::readSensorCom()
{
    static QByteArray buf;

    buf += sensorCom.readAll();
    if(buf.indexOf("\r\n") != -1)
    {
        qDebug() << buf;
        sensor.decodeResponse(buf);
        ui->label_5->setText(sensor.get_temperature()); //"实时温度：" +
//        ui->comboBox->setCurrentText(sensor.get_poll_burst_mode());//"传输模式：" +
//        ui->comboBox_2->setCurrentText(sensor.get_display_mode_mr());//"单、双色：" +
//        ui->doubleSpinBox->setValue(sensor.get_peak_hold_time().toDouble());//"峰值保持：" +
        buf.clear();
    }
}

void MainWindow::readIpcCom()
{

}

void MainWindow::on_pushButton_clicked()
{
    sensorCom.write(sensor.set_poll_burst_mode(ui->comboBox->currentText()).toLatin1());
    sleep(100);
    sensorCom.write(sensor.set_display_mode_mr(ui->comboBox_2->currentIndex() + 1).toLatin1());
    sleep(100);
    if(ui->comboBox_2->currentIndex() == 0)
        sensorCom.write("$=UWSI\r");
    else
        sensorCom.write("$=UTSI\r");

    sleep(100);
    sensorCom.write(sensor.set_peak_hold_time(ui->doubleSpinBox->value()).toLatin1());
}

void MainWindow::on_pB_sensor_clicked()
{
    sensorCom.write((ui->lE_sensor->text() + "\r").toLatin1());
}

void MainWindow::setSensor()
{

}
